/*
 * @Author: shenghao.xu
 * @Date: 2023-04-11 19:43:37
 * @LastEditors: shenghao.xu
 * @LastEditTime: 2023-06-28 17:52:34
 * @Description:
 * email:545403892@qq.com
 * Copyright (c) 2023 by shenghao.xu, All Rights Reserved.
 */

#include "board.h"
#include "usart.h"
#include "delay.h"
#include "uart.h"
#include "hart.h"
RTC_DateTypeDef GetDate;
RTC_TimeTypeDef GetTime;

// 串口1接收中断回调函数，rs232串口
static void uart1_rx_cb(UART_HandleTypeDef *huart)
{
    uart_t *handle = UartGetHandle(UART_1);
    // 串口1接收HART数据，因为是空闲中断处理这里只过滤掉前导码
    hart_handle(handle->rx_buffer, handle->rxIndex);
}

void uart1_send_data(uint8_t *data, uint16_t len)
{
    UartSend(UART_1, data, len);
}

/*------------------------------------------------------------
Func: EEPROM  Read
Note:
-------------------------------------------------------------*/
void eeprom_read(uint32_t read_addr, uint8_t *data, uint16_t length)
{
    uint8_t *wAddr;
    wAddr = (uint8_t *)(read_addr);
    while (length--)
    {
        *data++ = *wAddr++;
    }
}

/*------------------------------------------------------------
Func: EEPROM Write
Note:
-------------------------------------------------------------*/
void eeprom_write(uint32_t write_addr, uint8_t *data, uint16_t length)
{
    uint16_t t;
    HAL_FLASHEx_DATAEEPROM_Unlock();
    for (t = 0; t < length; t++)
    {
        HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, write_addr + t, *(data + t));
    }
    HAL_FLASHEx_DATAEEPROM_Unlock();
}

/**
 * @description: 板卡初始化
 * @return {*}
 */
void board_init(void)
{
    // 继电器使能
    RELAY_EN_ON();

    // 232串口初始化开始
    UartInit(UART_1, &huart1, TRUE, 255, uart1_rx_cb, TRUE, 0);
    UARTReceiveIT(UART_1);
    // 串口初始化结束
}
